﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using GameEngine;
using GameEngine.PDNParser;

namespace Daphne.Controls
{
    /// <summary>
    /// Interaction logic for PDNHeaderEditor.xaml
    /// </summary>
    public partial class PDNHeaderEditor : UserControl
    {
        public PDNHeaderEditor()
        {
            InitializeComponent();

            uxEvent.TextChanged += uxEvent_TextChanged;
            uxSite.TextChanged += uxSite_TextChanged;
            uxWhite.TextChanged += uxWhite_TextChanged;
            uxBlack.TextChanged += uxBlack_TextChanged;
            uxRound.TextChanged += uxRound_TextChanged;
            uxResult.TextChanged += uxResult_TextChanged;
        }

        void uxResult_TextChanged(object sender, TextChangedEventArgs e)
        {
            UpdateValue(PDNHeaderKey.Result, uxResult.Text);
            e.Handled = true;
        }

        void uxRound_TextChanged(object sender, TextChangedEventArgs e)
        {
            UpdateValue(PDNHeaderKey.Round, uxRound.Text);
            e.Handled = true;
        }

        void uxBlack_TextChanged(object sender, TextChangedEventArgs e)
        {
            UpdateValue(PDNHeaderKey.Black, uxBlack.Text);
            e.Handled = true;
        }

        void uxWhite_TextChanged(object sender, TextChangedEventArgs e)
        {
            UpdateValue(PDNHeaderKey.White, uxWhite.Text);
            e.Handled = true;
        }

        void uxSite_TextChanged(object sender, TextChangedEventArgs e)
        {
            UpdateValue(PDNHeaderKey.Site, uxSite.Text);
            e.Handled = true;
        }

        void uxEvent_TextChanged(object sender, TextChangedEventArgs e)
        {
            UpdateValue(PDNHeaderKey.Event, uxEvent.Text);
            e.Handled = true;
        }

        public GameManager GameManager
        { get; set; }

        public void Update()
        {
            uxEvent.Text = GetHeader(PDNHeaderKey.Event);
            uxSite.Text = GetHeader(PDNHeaderKey.Site);
            uxWhite.Text = GetHeader(PDNHeaderKey.White);
            uxBlack.Text = GetHeader(PDNHeaderKey.Black);
            uxRound.Text = GetHeader(PDNHeaderKey.Round);
            uxResult.Text = GetHeader(PDNHeaderKey.Result);
        }

        private void UpdateValue(string sKey, string sValue)
        {
            if (GameManager == null)
                return;

            GameManager.PDNHeaders[sKey] = sValue;
        }

        private string GetHeader(string sKey)
        {
            if (GameManager == null)
                return string.Empty;
            
            string sValue;
            if (GameManager.PDNHeaders.TryGetValue(sKey, out sValue) == false)
                return string.Empty;
            return sValue;
        }

        
    }
}
